﻿
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Tetris.Interfaces;

namespace Tetris.Blocks
{
    // ReSharper disable InconsistentNaming
    public class LBlock : AbstractBlock
    // ReSharper restore InconsistentNaming
    {
        // '|
        private readonly ITile _tileTopLeft;
        private readonly ITile _tileTop;
        private readonly ITile _tileMiddle;
        private readonly ITile _tileBottom;

        public LBlock(Texture2D texture, Color color, Vector2 position, int fallDelay)
        {
            Texture = texture;
            Color = color;
            Tiles = new List<ITile>();

            // setup skeleton
            _tileTopLeft = new Tile(Texture, Color);
            _tileTop = new Tile(Texture, Color);
            _tileMiddle = new Tile(Texture, Color);
            _tileBottom = new Tile(Texture, Color);
            Tiles.AddRange(new[] { _tileTopLeft, _tileTop, _tileMiddle, _tileBottom });

            Position = new Vector2(position.X, position.Y);
            FallDelay = fallDelay;
            LastFallen = 0;
        }

        protected override void ReArrangeParts(Vector2 position, RotationAngle angle)
        {
            if (angle == RotationAngle.South)
            {
                _tileTopLeft.Position = new Vector2(position.X + Texture.Width, position.Y - Texture.Height);
                _tileTop.Position = new Vector2(position.X + Texture.Width, position.Y);
                _tileMiddle.Position = new Vector2(position.X, position.Y);
                _tileBottom.Position = new Vector2(position.X - Texture.Width, position.Y);
            }
            else if (angle == RotationAngle.East)
            {
                _tileTopLeft.Position = new Vector2(position.X + Texture.Width, position.Y + Texture.Height);
                _tileTop.Position = new Vector2(position.X, position.Y + Texture.Height);
                _tileMiddle.Position = new Vector2(position.X, position.Y);
                _tileBottom.Position = new Vector2(position.X, position.Y - Texture.Height);
            }
            else if (angle == RotationAngle.North)
            {
                _tileTopLeft.Position = new Vector2(position.X - Texture.Width, position.Y + Texture.Height);
                _tileTop.Position = new Vector2(position.X - Texture.Width, position.Y);
                _tileMiddle.Position = new Vector2(position.X, position.Y);
                _tileBottom.Position = new Vector2(position.X + Texture.Width, position.Y);
            } 
            else
            {
                _tileTopLeft.Position = new Vector2(position.X - Texture.Width, position.Y - Texture.Height);
                _tileTop.Position = new Vector2(position.X, position.Y - Texture.Height);
                _tileMiddle.Position = new Vector2(position.X, position.Y);
                _tileBottom.Position = new Vector2(position.X, position.Y + Texture.Height);
            }

        }

        public override void Rotate()
        {
            CurrentAngle = GetNextRotationAngle();
            ReArrangeParts(Position, CurrentAngle);
        }
    }
}
